<?php
class OLM_TemplatePrint
{
	static function standard($sales_order_ids)
	{
		set_time_limit(0);
		require_once(Q::ini('custom_system/lib_dir') . 'tcpdf/config/lang/eng.php');
		require_once(Q::ini('custom_system/lib_dir') . 'tcpdf/tcpdf.php');
		$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, array(148, 210), true, 'UTF-8', false);
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
		$pdf->SetMargins(1, 1, 1);
		$pdf->SetPrintHeader(false);
		$pdf->SetPrintFooter(false);
		$pdf->SetAutoPageBreak(true, 0);
		$pdf->SetLanguageArray($l);
		$sales_order = Sales_Order::find('sales_order.id in (?)', $sales_order_ids)
			->joinLeft('sales_order_customer_info', 'shop_buyer_id, fullname', 'sales_order_customer_info.sales_order_id=sales_order.id')
			->joinLeft('shop', 'name as shop_name', 'shop.id=sales_order.shop_id')
			->joinLeft('shipping_method', 'name as shipping_method_name', 'shipping_method.id=sales_order.shipping_method')
			->order('sales_order.sort_shelf_position asc')->asArray()->getAll();
		foreach ($sales_order as $key => $so)
		{
			$item = Sales_Order_Item::find('sales_order_id=?', $so['id'])
				->joinLeft('product_warehouse', 'storage_number, shelf_position, product_feature', 'product_warehouse.product_basic_id=sales_order_item.product_basic_id and product_warehouse.warehouse_id=' . $so['warehouse_id'])
				->joinLeft('product_basic', 'name as product_name', 'product_basic.id=sales_order_item.product_basic_id')
				->order('product_warehouse.shelf_position')->asArray()->getAll();
			$sales_order[$key]['item'] = $item;
		}
		foreach ($sales_order as $o)
		{
			$pack_time = CURRENT_TIMESTAMP;
			if ($o['send_method'] <> Q::ini('custom_flag/sales_order_send_method/resend/value'))
			{
				$pack_time = ((CURRENT_TIMESTAMP - strtotime($o['sales_time']))/86400>2) ? (strtotime($o['sales_time'])+2*86400) : CURRENT_TIMESTAMP;
			}
			$pdf->AddPage();
			$pdf->SetCellPadding(0);
			$pdf->SetFont('msyh', '', 20);
			$pdf->write1DBarcode($o['number'], 'C128', '', '', 148, 20, 0.5, array('align' => 'C', 'text' => true, 'stretchtext' => 3, 'cellfitalign' => 'C'), 'C');
			$pdf->Ln(10);
			$pdf->SetFont('msyh', '', 12);
			$pdf->MultiCell('', '', '      ' . $o['shipping_method_name'], 0, 'L', 0, 1, 0, '', true);
			$pdf->Line(142,  $pdf->GetY(), 5, $pdf->GetY());
			$pdf->Ln(2.5);
			$pdf->MultiCell(0, 0, '      ' . ucfirst($o['shop_name']) . "\n" . '      ' . ucfirst($o['shop_buyer_id'] ? $o['shop_buyer_id'] : $o['fullname']) . "\n" . '      Order Time: ' . date('Y-m-d T', strtotime($o['sales_time'])). "\n      Pack Time: " . date('Y-m-d T', $pack_time) . "\n ", 0, 'L', 0, 1, 0, '', true);
			$pdf->Line(142,  $pdf->GetY()-3, 5, $pdf->GetY()-3);
			$pdf->SetFont('msyh', '', 12);
			$pdf->MultiCell('', '', '      NO.                       SKU                         Qty                       ShelfNO.', 0, 'L', 0, 0, '', '', true);
			$pdf->Line(142,  $pdf->GetY()+8.5, 5, $pdf->GetY()+8.5);
			$pdf->Ln(12);
			foreach ($o['item'] as $k => $oi)
			{
				$pdf->SetCellPadding(1);
				$pdf->MultiCell(20, 15, $k+1, 0, 'C', 0, 0, '', '', true);
				$pdf->MultiCell(60, 15, $oi['product_name'] . ' ' . $oi['storage_number'], 0, 'C', 0, 0, '', '', true);
				$pdf->MultiCell(15, 15, $oi['quantity'], 0, 'C', 0, 0, '', '', true);
				$pdf->MultiCell(45, 15, $oi['shelf_position'] . ' ' . $oi['product_feature'], 0, 'R', 0, 1, '', '', true);
			}
			$pdf->Line(142, $pdf->GetY()+3, 5, $pdf->GetY()+3);
			$pdf->Ln(5);
			$pdf->MultiCell('', '', '     Total' . '                                                       ' .$o['product_quantity'], 0, 'L', 0, 1, '', '', true);
			$pdf->Line(142, $pdf->GetY()+2, 5, $pdf->GetY()+2);
			$pdf->Ln(5);
			$pdf->SetFont('msyh', '', 10);
			$pdf->MultiCell('', '', "Thanks for your purchase. It's our pleasure to do business with you and we hope you're satisfied with our products and services. Don't hesitate to contact us if you have any questions.We'll try our best to assist. Thank you very much again.", 0, 'L', 0, 1, 4, '', true);
		}
		$pdf->Output('pdf.pdf');
	}
	
	static function DHL($sales_order_ids)
	{
		require_once(Q::ini('custom_system/lib_dir') . 'tcpdf/config/lang/eng.php');
		require_once(Q::ini('custom_system/lib_dir') . 'tcpdf/tcpdf.php');
		$pdf = new TCPDF('L', PDF_UNIT, array(230, 150), true, 'UTF-8', false);
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
		$pdf->SetMargins(1, 1, 1);
		$pdf->SetPrintHeader(false);
		$pdf->SetPrintFooter(false);
		$pdf->SetAutoPageBreak(true, 1);
		$pdf->SetLanguageArray($l);
		$pdf->SetFont('droidsansfallback', '', 12);
		$columns = array(
				'sales_order_customer_info.*',
				'sales_order.number, sales_order.weight'
		);
		$sales_order = Sales_Order::find('sales_order.id in (?)', $sales_order_ids)
			->joinLeft('sales_order_customer_info', '', 'sales_order_customer_info.sales_order_id=sales_order.id')
			->setColumns($columns)->asArray()->getAll();
		foreach ($sales_order as $key => $so)
		{
			$pdf->AddPage();
			$sales_order[$key]['quantity'] = 0;
			$sales_order[$key]['declared_value'] = 0;
			$item = Sales_Order_Item::find('sales_order_id=?', $so['sales_order_id'])
				->joinLeft('product_basic', 'declared_value, declared_name_cn, declared_name_en, weight', 'product_basic.id=sales_order_item.product_basic_id')
				->asArray()->getAll();
			$pdf->MultiCell(90, 105, '', 0, 'L', 0, 1);
			$pdf->MultiCell(90, 28, $sales_order[$key]['street1'] .'  '. $sales_order[$key]['street2'] . "\n" . $sales_order[$key]['city'] . "\n" . $sales_order[$key]['state'], 0, 'L', 0, 1);
			$pdf->MultiCell(45, 8, $sales_order[$key]['zip'], 0, 'L', 0, 0);
			$pdf->MultiCell(45, 8, $sales_order[$key]['country'], 0, 'L', 0, 1);
			$pdf->MultiCell(45, 8, $sales_order[$key]['fullname'], 0, 'L', 0, 0);
			$pdf->MultiCell(45, 8, $sales_order[$key]['phone'], 0, 'L', 0, 0);
			$pdf->MultiCell(20, 5, round($sales_order[$key]['weight']/1000, 4), 0, 'C', 0, 1, 100, 50);
			$pdf->Ln();
		}
		$pdf->Output('pdf.pdf');
	}
	
	static function EMS($sales_order_ids)
	{
		require_once(Q::ini('custom_system/lib_dir') . 'tcpdf/config/lang/eng.php');
		require_once(Q::ini('custom_system/lib_dir') . 'tcpdf/tcpdf.php');
		$pdf = new TCPDF('L', PDF_UNIT, array(240, 150), true, 'UTF-8', false);
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
		$pdf->SetMargins(1, 1, 1);
		$pdf->SetPrintHeader(false);
		$pdf->SetPrintFooter(false);
		$pdf->SetAutoPageBreak(true, 1);
		$pdf->SetLanguageArray($l);
		$pdf->SetFont('droidsansfallback', '', 10);
		
		$columns = array(
				'sales_order_customer_info.*',
				'sales_order.number, sales_order.weight'
		);
		$sales_order = Sales_Order::find('sales_order.id in (?)', $sales_order_ids)
			->joinLeft('sales_order_customer_info', '', 'sales_order_customer_info.sales_order_id=sales_order.id')
			->setColumns($columns)->asArray()->getAll();
		foreach ($sales_order as $key => $so)
		{
			$pdf->AddPage();
			$sales_order[$key]['quantity'] = 0;
			$sales_order[$key]['declared_value'] = 0;
			$item = Sales_Order_Item::find('sales_order_id=?', $so['sales_order_id'])
				->joinLeft('product_basic', 'declared_value, declared_name_cn, declared_name_en, weight', 'product_basic.id=sales_order_item.product_basic_id')
				->asArray()->getAll();
			$sales_order[$key]['declared_name_cn'] = $item[0]['declared_name_cn'];
			$sales_order[$key]['declared_name_en'] = $item[0]['declared_name_en'];
			foreach ($item as $i)
			{
				$sales_order[$key]['quantity'] += $i['quantity'];
				$sales_order[$key]['declared_value'] += $i['declared_value'];
			}
			$pdf->setCellMargins(0, 111, 0, 0);
			$pdf->MultiCell(46, 10, $sales_order[$key]['declared_name_cn'] . ' ' . $sales_order[$key]['declared_name_en'] , 0, 'C', 0, 0);
			$pdf->MultiCell(10, 10, $sales_order[$key]['quantity'], 0, 'R', 0, 0);
			$pdf->MultiCell(15, 10, $sales_order[$key]['weight']/1000, 0, 'R', 0, 0);
			$pdf->MultiCell(10, 10, $sales_order[$key]['declared_value'], 0, 'R', 0, 0);
			$pdf->setCellMargins(30, 22, 0, 0);
			$pdf->MultiCell(100, 12, $sales_order[$key]['fullname'], 0, 'L', 0, 1);
			$pdf->setCellMargins(110, 0, 0, 0);
			$pdf->MultiCell(100, 10, $sales_order[$key]['city'] . '                            ' . $sales_order[$key]['country_cn'] .' / '. $sales_order[$key]['country'] . "\n\n\n\n", 0, 'L', 0, 1);
			$pdf->MultiCell(100, 20, $sales_order[$key]['street1'] .'  '. $sales_order[$key]['street2'], 0, 'L', 0, 1);
			$pdf->MultiCell(100, 10, $sales_order[$key]['zip'] . '                                      ' . $sales_order[$key]['phone'], 0, 'L', 0, 1);
		}
		$pdf->Output('pdf.pdf');
	}
}